<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020\4\8 0008
  Time: 14:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="/js/jquery.js"></script>
    <script>
        $(function () {
            $("#submit").click(function () {
                var arr = [];//定义一个空数组
                $("[name='hobbies']:checked").each(function(i){//把所有被选中的复选框的值存入数组
                    arr[i] =$(this).val();
                });
                $.ajax({
                    url: "/user",
                    /*发送到处理端的数据，name必须跟checkUser参数名相同 */
                    data: {"name": $("#name").val(),
                        "hobbies":arr,
                        "addr.province":$("#pro").val(),
                        "addr.city":$("#city").val(),
                        "birth":$("#birth").val()
                    },
                    traditional:true,//必须添加这个参数，用来处理数组数据，使数组中每个元素都使用的key
                    success: function (data) {
                        $("#name").val(data.name);
                        $("#city").val(data.addr.city);
                        if(data.hobbies==null||data.hobbies[0]==null){
                            $("[value=旅游]").prop("checked",false);
                        }else {
                            $("[value=旅游]").prop("checked",true);
                        }
                        $("#birth").val(data.birthday);
                    }
                });
            });
        });
    </script>
</head>
<body>
    用户名:<input name="name" id="name"/>
    <br>
    省:<input name="addr.province" id="pro" />市:<input name="addr.city" id="city" value="${addr.city}"/>
    <br>
    旅游<input type="checkbox" name="hobbies" value="旅游" >
    美食<input type="checkbox" name="hobbies" value="美食">
    <br>
    生日:<input id="birth" name="birth" placeholder="yyyy-MM-dd">
    <hr>
    <input type="button" id="submit">
</body>
</html>
